const app = getApp();

let methods = {
  getActivtyAndCoupont,
  joinInActivtyAndCoupont,
};

var component = {
  properties: {
    //活动类型：
    // 1.登陆获取首单立减
    // 2.邀请好友
    // 0.(支付订单前三名订单、已激活订单、未激活预订单相关的三种类型，订单orderUuid不能为空)
    // 6.智能团课排名
    // 7.兑换优惠券
    // 8.运动数据获取优惠券
    activityTypeId: null,
  },
  data: {
    xhx_images: app.xhx_images,
    xhx_route: app.xhx_route,
  },
  ready:show,
  pageLifetimes: {
    show,
  },
  methods: Object.assign(app.navToNext, methods)
};

Component(component);

function show() {
  const that = this;
  app.xlogin.getUser(function(res) {
    that.getActivtyAndCoupont();
  });
}

function getActivtyAndCoupont() {
  const that = this;
  const activityTypeId = that.data.activityTypeId;

  if (activityTypeId != 6) return;
  app.middleware.requestAjax({
    serAddr: app.serAddrs.getActivtyAndCoupont,
    data: {
      activityTypeId,
    },
    cancelToast: true,
    success: function(res) {
      let couponsList = res.data || [];
      that.setData({
        couponsList,
      })
    },
    empty: function() {
      let couponsList = [];
      that.setData({
        couponsList,
      })
    },
    fail: function(res) {

    }
  })
}

function joinInActivtyAndCoupont(event) {
  const that = this;
  const activityTypeId = that.data.activityTypeId;
  const couponsList = that.data.couponsList || [];
  const index = event.currentTarget.dataset.index;
  const activityUuid = couponsList[index].activityUuid;
  const lectureUuid = couponsList[index].lectureUuid;
  const couponUuid = couponsList[index].couponUuid;
  console.log(couponsList[index])
  app.middleware.requestAjax({
    serAddr: app.serAddrs.joinInActivtyAndCoupont,
    data: {
      activityTypeId,
      activityUuid,
      lectureUuid,
      couponUuid,
    },
    cancelToast: true,
    success: function(res) {
      app.routePush(`${app.xhx_route.leagueRank}?activityUuid=${activityUuid}&lectureUuid=${lectureUuid}&couponUuid=${couponUuid}`);
    },
    fail: function(res) {
      app.showToast(res.msg);
    }
  })
}